package com.juhuacha.tryrxjava;

import android.os.Bundle;
import android.os.Looper;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.jakewharton.rxbinding.widget.RxTextView;
import com.jakewharton.rxbinding.widget.TextViewTextChangeEvent;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class SearchAct extends AppCompatActivity {

    EditText mEditText;
    ListView mListView;

    private List<String> mDatas;
    private TestAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        mEditText = (EditText) findViewById(R.id.et_search);
        mListView = (ListView) findViewById(R.id.lv_list);
        mDatas = new ArrayList<>();
        mAdapter = new TestAdapter();

        mListView.setAdapter(mAdapter);

        RxTextView.textChangeEvents(mEditText)
                .debounce(300, TimeUnit.MILLISECONDS)
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .filter(new Func1<TextViewTextChangeEvent, Boolean>() {
                    @Override
                    public Boolean call(TextViewTextChangeEvent textViewTextChangeEvent) {
                        boolean filter = !TextUtils.isEmpty(textViewTextChangeEvent.text());
                        if(Thread.currentThread() == Looper.getMainLooper().getThread()){
                            //确认在主线程
                            mDatas.clear();
                            mAdapter.notifyDataSetChanged();
                        }
                        return filter;
                    }
                })
                .switchMap(new Func1<TextViewTextChangeEvent, Observable<List <String>>>() {
                    @Override
                    public Observable<List <String>> call(final TextViewTextChangeEvent textViewTextChangeEvent) {
                        return Observable.create(new Observable.OnSubscribe<List<String>>() {
                            @Override
                            public void call(Subscriber<? super List<String>> subscriber) {
                                SystemClock.sleep(1000);
                                //这里是网络请求操作...
                                List<String> datas = new ArrayList<>();
                                for (int i = 0; i < 10; i++) {
                                    datas.add("KeyWord:" + textViewTextChangeEvent.text().toString().trim() + i);
                                }
                                subscriber.onNext(datas);
                                subscriber.onCompleted();
                            }
                        });
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<List<String>>() {
                    @Override
                    public void call(List<String> strings) {
                        initPage(strings);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {

                    }
                });
    }
    private void initPage(List<String> keyWords) {
        if (mAdapter == null) {
            mAdapter = new TestAdapter();
            mListView.setAdapter(mAdapter);
            //lv_list.setOnItemClickListener(itemClick());
        } else {
            mDatas.clear();
            mDatas.addAll(keyWords);
            mAdapter.notifyDataSetChanged();
        }
    }
    class TestAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return mDatas != null ? mDatas.size() : 0;
        }

        @Override
        public Object getItem(int i) {
            return mDatas.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View ret = LayoutInflater.from(SearchAct.this).inflate(R.layout.item_layout,viewGroup,false);
            TextView textView = (TextView)ret.findViewById(R.id.item_text);
            textView.setText(mDatas.get(i));
            return ret;
        }

    }
}
